<?php

/**
 * user actions.
 *
 * @package    workforoptions
 * @subpackage user
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class userActions extends sfJSONActions
{
  /**
   * Executes index action
   *
   */
  public function executeIndex()
  {
    $this->forward('default', 'module');
  }
  
  public function executeProfile()
  {
  
  $this->author = UserPeer::retrieveByPK($this->getRequestParameter('id'));
  
  }
  
  public function executeLogin()
  {
  
	if ($this->getRequest()->getMethod() != sfRequest::POST)
    {
    
    if ($this->getContext()->getUser()->isAuthenticated()) {

		return $this->redirect('/startupindex');

		}
    
      // display the form
      $this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
    }
    else
    {
    
  		if ($this->getContext()->getUser()->isAuthenticated()) {

		return $this->redirect('/startupindex');

		}
    
    
    }
  
  }
  
    public function executeLogout()
  {
    $this->getUser()->signOut();
    $this->redirect('/startupindex');
  }
  
  
  public function handleErrorLogin()
  {
    return sfView::SUCCESS;
  }
  
   public function handleErrorEdit()
  {
    return sfView::SUCCESS;
  }
  
   
  public function handleErrorSignup()
  {
    return sfView::SUCCESS;
  }
  
   public function handleErrorResetPassword()
  {
    return sfView::SUCCESS;
  }
  
   public function executeEdit()
  {
        
   if ($this->getRequest()->getMethod() != sfRequest::POST)
	    {
	      // display the form
	     $this->author = $newUser = UserPeer::retrieveByPK($this->getContext()->getUser()->getSignedUserId());

	      $this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
	    }
	    else
	    {
  
			  
			   
		    $this->author = $newUser = UserPeer::retrieveByPK($this->getContext()->getUser()->getSignedUserId());

		  //update user
		  	  if($this->getRequestParameter('type')  != ''){$newUser->setType($this->getRequestParameter('type'));}
		      if($this->getRequestParameter('firstName')  != ''){$newUser->setFirstName($this->getRequestParameter('firstName'));}
		      if($this->getRequestParameter('lastName')  != ''){$newUser->setLastName($this->getRequestParameter('lastName'));}
		      if($this->getRequestParameter('password_1')  != ''){$newUser->setPassword($this->getRequestParameter('password_1'));}
		      if($this->getRequestParameter('email') != ''){$newUser->setEmail($this->getRequestParameter('email'));}
		      if($this->getRequestParameter('ventures') != ''){$newUser->setVentures($this->getRequestParameter('ventures'));}
		      if($this->getRequestParameter('age') != ''){$newUser->setAge($this->getRequestParameter('age'));}
		      if($this->getRequestParameter('website') != ''){$newUser->setWebsite($this->getRequestParameter('website'));}
		      if($this->getRequestParameter('location') != ''){$newUser->setLocation($this->getRequestParameter('location'));}
		      if($this->getRequestParameter('linkedin') != ''){$newUser->setLinkedIn($this->getRequestParameter('linkedin'));}
		      if($this->getRequestParameter('facebook') != ''){$newUser->setFaceBook($this->getRequestParameter('facebook'));}
		      if($this->getRequestParameter('yahoo') != ''){$newUser->setYahoo($this->getRequestParameter('yahoo'));}
		      if($this->getRequestParameter('msn') != ''){$newUser->setMsn($this->getRequestParameter('msn'));}
		      if($this->getRequestParameter('icq') != ''){$newUser->setIcq($this->getRequestParameter('icq'));}
		      if($this->getRequestParameter('skype') != ''){$newUser->setSkype($this->getRequestParameter('skype'));}
		      if($this->getRequestParameter('gtalk') != ''){$newUser->setGtalk($this->getRequestParameter('gtalk'));}
		      if($this->getRequestParameter('aim') != ''){$newUser->setAim($this->getRequestParameter('aim'));}
		      if($this->getRequestParameter('twitter')){$newUser->setTwitter($this->getRequestParameter('twitter'));}
			  if($this->getRequestParameter('raisedmoney') != ''){$newUser->setRaisedMoney($this->getRequestParameter('raisedmoney'));}	
			  if($this->getRequestParameter('raisedmoneyhowmuch')){$newUser->setVentureRaised($this->getRequestParameter('raisedmoneyhowmuch'));}
			  if($this->getRequestParameter('ready2relocate')  != ''){$newUser->setRelocation($this->getRequestParameter('ready2relocate'));}
			  if($this->getRequestParameter('yearsexperience') != ''){$newUser->setYexperience($this->getRequestParameter('yearsexperience'));}
			  if($this->getRequestParameter('canworkas') != ''){$newUser->setCanWorkAs($this->getRequestParameter('canworkas'));}
			  if($this->getRequestParameter('resumeurl') != ''){$newUser->setResumeUrl($this->getRequestParameter('resumeurl'));}
			  if($this->getRequestParameter('expectedequity') != ''){$newUser->setExpEquity($this->getRequestParameter('expectedequity'));}
			  if($this->getRequestParameter('ready2commit') != ''){$newUser->setReadyCommitment($this->getRequestParameter('ready2commit'));}		      	  if($this->getRequestParameter('linkedin')){$newUser->setLinkedin($this->getRequestParameter('linkedin'));}		      
		      if($this->getRequestParameter('state')){$newUser->setState($this->getRequestParameter('state'));}		      
		      if($this->getRequestParameter('zipcode')){$newUser->setZipCode($this->getRequestParameter('zipcode'));}	
					  		     
			  $newUser->save();
			 
	     	if ($this->getRequest()->getFileName('logourl') != '') {
			
			$fileUploaded = $this->getRequest()->getFileName('logourl');
			$fileend = utils::findexts($fileUploaded);
		    $fileName = $newUser->getFullName();
		    $fileName .= '_thumb_';
			$fileName .= '.';
			$fileName .= $fileend;

			$thumbnail = new sfThumbnail(85, 85, true, true, 105, 'sfGDAdapter');
			$thumbnail->loadFile($this->getRequest()->getFilePath('logourl'));
	  		$thumbnail->save('images/avatars/'.$fileName.'', 'image/jpeg');
		
		    
		    
		    $newUser->setAvatarUrl('/images/avatars/'.$fileName.'');
		    $newUser->save();
	    
			}
			  
			 return $this->redirect('/user/profile/?id='.$newUser->getId());


			
		}
		
	
  
  }

  
  
  
  public function executeSignup()
  {
  
   if ($this->getRequest()->getMethod() != sfRequest::POST)
	    {
	    
	      if (!$this->getContext()->getUser()->isAuthenticated()) {

	    
	      // display the form
	      $this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
	      
	      } else {
			
			return $this->redirect('/startupindex');

			
			}

	      
	    }
	    else
	    {
  		   
		  
		      if (!$this->getContext()->getUser()->isAuthenticated()) {
		  
		  
		  //create new user
		      $newUser = new User();
		      $newUser->setFirstName($this->getRequestParameter('firstName'));
		      $newUser->setLastName($this->getRequestParameter('lastName'));
		      $newUser->setPassword($this->getRequestParameter('password_1'));
		      $newUser->setEmail($this->getRequestParameter('email'));
		      $newUser->setAvatarId(0);
		      $newUser->save();
		  
		    if ($this->getRequest()->getFileName('logourl') != '') {
			
			$fileUploaded = $this->getRequest()->getFileName('logourl');
			$fileend = utils::findexts($fileUploaded);
		    $fileName = $newUser->getFullName();
		    $fileName .= '_thumb_';
			$fileName .= '.';
			$fileName .= $fileend;

			$thumbnail = new sfThumbnail(85, 85, true, true, 105, 'sfGDAdapter');
			$thumbnail->loadFile($this->getRequest()->getFilePath('logourl'));
	  		$thumbnail->save('images/avatars/'.$fileName.'', 'image/jpeg');
		
		    
		    
		    $newUser->setAvatarUrl('/images/avatars/'.$fileName.'');
		    $newUser->save();
	    
			}
		      
		      
			  //Mail the user
			  
			$body ='Hello '.$newUser->getFullName().',<br>
Welcome, Here are your login details:<br>
Login: '.$this->getRequestParameter('email').'<br>
Password: '.$this->getRequestParameter('password_1').'';
			  
			  
			  sfNotifications::mailAnonUser($newUser->getEmail(),'Welcome to 140index!',$body);
	  
			  $this->getContext()->getUser()->signIn($newUser);
			  return $this->redirect('/startupindex');
			  
			} else {
			
			return $this->redirect('/startupindex');

			
			}
			
		}
  
  }
  
 public function executeResetPassword()
 {
 
  if ($this->getRequest()->getMethod() != sfRequest::POST)
	    {
	      // display the form

	      $this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
	    }
	    else
	    {
  
  
  			  $c = new Criteria();
  			  $c->add(UserPeer::EMAIL,$this->getRequestParameter('email'));
			  $newUser = UserPeer::doSelectOne($c);
			  
			  if ($newUser) {
			  
			  $password = substr(md5(rand(100000, 999999)), 0, 6);
		      $newUser->setPassword($password);
			  $newUser->save();
 
 
  //Mail the user
			  
$body ='Hello '.$newUser->getFullName().',<br>
Welcome to the TechIndex community, Here are your NEW login details:<br>
Login: '.$newUser->getEmail().'<br>
Password: '.$password.'<br>';
			  
			  
			  sfNotifications::mailAnonUser($newUser->getEmail(),'Password reset for TechIndex!',$body);
	  
			  return $this->redirect('/');
			  }
		}
 
 
 }
  
  public function executeInviteUser() {
  
   if ($this->getRequest()->getMethod() != sfRequest::POST)
	    {
	      // display the form

	      $this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
	    }
	    else
	    {
  

  //Mail the user
			  
$body ='Hello,<br>
'.$this->getUser()->getFullName().' thought that you will be interested in a job listing on workforoptions.com.<br>
<a href="http://www.workforoptions.com/post/show/?id='.$this->getRequestParameter('postid').'">Click here to view it.</a><br><br>The Workforoptions team.<br><img src="http://www.workforoptions.com/images/logo.gif">';
			  
			  
		sfNotifications::mailAnonUser($this->getRequestParameter('email'),'Invite to workforoptions.com!',$body);
		return $this->returnJSON(true);

			  
		}		  
  
  }
  
  public function executeConnectFbUser()
 {
 	require_once('fbconnect/facebookclient/facebook.php'); 
	/* initialize the facebook API with your application API Key and Secret */ 
				
	$appapikey = 'a8149c1dbcc95a1a07bbc554f1901edf';
	$appsecret = '24184ac40d654738ef1a10425d55cce6';
	$facebook = new Facebook($appapikey, $appsecret);
	$fbuid = $facebook->get_loggedin_user();

	$user_details=$facebook->api_client->users_getInfo($fbuid, array('last_name','first_name','is_app_user','pic'));
				
	$data['uid']=$user_details[0]['uid'];
	$data['first_name']=$user_details[0]['first_name']; 
	$data['last_name']=$user_details[0]['last_name'];
	$data['isuser']=$user_details[0]['is_app_user'];
 	$data['picurl']=$user_details[0]['pic']; 	
 	
 	$newCheck = new Criteria();
	$newCheck->Add(user2facebookPeer::FBUID,$fbuid);
	$newFbUser = user2facebookPeer::doSelectOne($newCheck);
	
	if ($newFbUser) {
	
				$newCheckUser = new Criteria();
				$newCheckUser->Add(UserPeer::ID,$newFbUser->getUserId());
				$newUser = UserPeer::doSelectOne($newCheckUser);
				
				//Update the user details everytime
				$newUser->setFirstName($data['first_name']);
				$newUser->setLastName($data['last_name']);
				$newUser->setEmail('info@startupindex.com');
				$newUser->setAvatarUrl($data['picurl']);
				$newUser->setAvatarId(0);
				$newUser->save();
				

	
	} else {
	
				
				if ($newUser) {
					
				} else {
					
					$newUser = new User();
					$newUser->setFirstName($data['first_name']);
				    $newUser->setLastName($data['last_name']);
				    $newUser->setPassword('');
				    $newUser->setEmail('info@startupindex.com');
				    $newUser->setAvatarId(0);
				    $newUser->setAvatarUrl($data['picurl']);
				    $newUser->save();
				    
				    $newFbUserLink = new user2facebook();
					$newFbUserLink->setFbUid($data['uid']);
					$newFbUserLink->setUserId($newUser->getId());
					$newFbUserLink->save();
							
			
	

				
				
				}
		}
	
		$this->getContext()->getUser()->signIn($newUser);
		return $this->redirect('/');


	
 
 
 }
 
 public function executeDeleteCompany()
 {
 
 if ($this->getRequest()->getMethod() != sfRequest::POST)
	    {
	      // display the form
		$this->company = $company = CompanyPeer::retrieveByPk($this->getRequestParameter('id'));

	      $this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
	    } else {


		$this->company = $company = CompanyPeer::retrieveByPk($this->getRequestParameter('id'));
		$company->delete();

		return $this->redirect('/startupindex');

		}
 
 
 }
 
 public function executeNewCompany()
 {
 
 
  if ($this->getRequest()->getMethod() != sfRequest::POST)
	    {
	      // display the form

	      $this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
	    } else {
	    
	    //create a new company
	    
	    $newCompany = new Company();
	    $newCompany->setName(htmlentities($this->getRequestParameter('name'),ENT_QUOTES,'utf-8'));
	    $newCompany->setNameHebrew(htmlentities($this->getRequestParameter('hebrewname'),ENT_QUOTES,'utf-8'));
	    $newCompany->setDescription(htmlentities($this->getRequestParameter('description'),ENT_QUOTES,'utf-8'));
	    $newCompany->setFounded($this->getRequestParameter('founded'));
	    $newCompany->setEmail($this->getRequestParameter('email'));
	    $newCompany->setLocation($this->getRequestParameter('location'));
	    $newCompany->setUrl($this->getRequestParameter('url'));
	    $newCompany->setBlogUrl($this->getRequestParameter('blogurl'));
	    $newCompany->setRssUrl($this->getRequestParameter('rssurl'));
	    $newCompany->setTwitterUser($this->getRequestParameter('twitteruser'));
	    $newCompany->setPhoneNumber($this->getRequestParameter('phonenumber'));
	    $newCompany->setRounds($this->getRequestParameter('rounds'));
		$newCompany->save();
	    
	    //$newCompany->setFounders($this->getRequestParameter('founders'));
	    //Break the persons via commas, create a person, check before maybe there is someone like that.
	    
	    $foundersg = $this->getRequestParameter('founders');
	    
	    $founders = explode(',',$foundersg);
	    
	    foreach ($founders as $founder) {
	    	
		    $cfounderc = new Criteria();
		    $cfounderc->Add(PersonPeer::NAME,'%'.$founder.'%', Criteria::LIKE);
		    $cfounder = PersonPeer::doSelectOne($cfounderc);
		    
			    if (!$cfounder) {
			    	
			    $newFounder = new Person();
			    $newFounder->setName($founder);
			    $newFounder->save();
			    
			    $founderuser = $newFounder;
		
			    
			    	
			    } else {
			    	
			    $founderuser = $cfounder;
			    	
			    	
			    }
			//Lets attach the founder to company
	
			$newFounder2Company = new Person2Company();
			$newFounder2Company->setPersonId($founderuser->getId());
			$newFounder2Company->setCompanyId($newCompany->getId());
			$newFounder2Company->save();
		    
	    }
	    
	    
	    
	    $newCompany->setNumberEmploy($this->getRequestParameter('numberemploy'));
		$newCompany->setAuthorId($this->getContext()->getUser()->getSignedUserId());
		$newCompany->setPublishState('draft');
		$newCompany->setSlug(utils::toAscii(htmlentities($this->getRequestParameter('name'),ENT_QUOTES,'utf-8')));
		$newCompany->save();
		
		//Create a shadow version
		
		$newCompanyShadow = new CompanyShadow();
	    $newCompanyShadow->setName(htmlentities($this->getRequestParameter('name'),ENT_QUOTES,'utf-8'));	    	    
	    $newCompanyShadow->setNameHebrew(htmlentities($this->getRequestParameter('hebrewname'),ENT_QUOTES,'utf-8'));
	    $newCompanyShadow->setDescription(htmlentities($this->getRequestParameter('description'),ENT_QUOTES,'utf-8'));
	    $newCompanyShadow->setFounded($this->getRequestParameter('founded'));
	    $newCompanyShadow->setEmail($this->getRequestParameter('email'));
	    $newCompanyShadow->setLocation($this->getRequestParameter('location'));
	    $newCompanyShadow->setUrl($this->getRequestParameter('url'));
	    $newCompanyShadow->setBlogUrl($this->getRequestParameter('blogurl'));
	    $newCompanyShadow->setRssUrl($this->getRequestParameter('rssurl'));
	    $newCompanyShadow->setTwitterUser($this->getRequestParameter('twitteruser'));
	    $newCompanyShadow->setPhoneNumber($this->getRequestParameter('phonenumber'));
	    $newCompanyShadow->setRounds($this->getRequestParameter('rounds'));
	    $newCompanyShadow->setFounders($this->getRequestParameter('founders'));
	    $newCompanyShadow->setNumberEmploy($this->getRequestParameter('numberemploy'));
		$newCompanyShadow->setAuthorId($this->getContext()->getUser()->getSignedUserId());
		$newCompanyShadow->setPublishState('draft');
		$newCompanyShadow->setSlug(utils::toAscii(htmlentities($this->getRequestParameter('name'),ENT_QUOTES,'utf-8')));
		$newCompanyShadow->setCompanyId($newCompany->getId());
		$newCompanyShadow->setStatus(1);
		$newCompanyShadow->setVersion(1);
		$newCompanyShadow->save();
		
		//Save the new shadow and link it
		$newCompany->setVersionshadow($newCompanyShadow->getId());
		$newCompany->save();
		
		
	    if ($this->getRequest()->getFileName('logourl') != '') {
		
		$fileUploaded = $this->getRequest()->getFileName('logourl');
		//$fileEnd = explode(".", $fileUploaded);
		$fileend = utils::findexts($fileUploaded);
	    $fileName = $newCompany->getName();
	    $fileName .= $newCompany->getUpdatedAt();
	    $fileName .= '_thumb_';
		$fileName .= '.';
		$fileName .= $fileend;
		//$fileName .= 'jpg';
		$thumbnail = new sfThumbnail(105, 105, true, true, 105, 'sfGDAdapter');
		$thumbnail->loadFile($this->getRequest()->getFilePath('logourl'));
  		$thumbnail->save('images/companylogos/'.$fileName.'', 'image/jpeg');
	    
	 	$fileNameBig = $newCompany->getName();
	 	$fileNameBig .= $newCompany->getUpdatedAt();
	    $fileNameBig .= '_big_';
		$fileNameBig .= '.';
		$fileNameBig .= $fileend;
		//$fileNameBig .= 'jpg';
		$thumbnailbig = new sfThumbnail(225, 155, true, true, 155, 'sfGDAdapter');
		$thumbnailbig->loadFile($this->getRequest()->getFilePath('logourl'));
  		$thumbnailbig->save('images/companylogos/'.$fileNameBig.'', 'image/jpeg');

	    
	    
	    $newCompany->setLogoUrl('/images/companylogos/'.$fileName.'');
	    $newCompany->save();
	    
	    $newCompanyShadow->setLogoUrl('/images/companylogos/'.$fileName.'');
	    $newCompanyShadow->save();	    
	    
		}
		//Lets create some tags for the seed funding
		
		
		  if ($this->getRequestParameter('rounds') == '') {
			//Lets do nothing bout the tags please
		  } else {
		   $seedrounds = $this->getRequestParameter('rounds');
		   $N = count($seedrounds);
		
		    //echo("You selected $N door(s): ");
		    for($i=0; $i < $N; $i++)
		    {
		      //echo($seedrounds[$i] . " ");
		      
		 	   $pat[0] = "/\s+/";
			
				$rep[0] = "";
			
				$str = preg_replace($pat,$rep,$seedrounds[$i]);
				
				if ($str != '') {
				$round = Tags::getTag($str);
				$newCompany2Tag = new Tag2Company();
				$newCompany2Tag->setTagId($round->getId());
				$newCompany2Tag->setCompanyId($newCompany->getId());
				$newCompany2Tag->save();
				}	
		    	
		    	
		    }

		  }	
		
		
		
		
		//Lets create some tags!
		
		
			  //Lets create some tags baby!
		
		  if ($this->getRequestParameter('tags') == '') {
			//Lets do nothing bout the tags please
		  } else {
		  $TagsCut = array();
		  
		  if (strstr($this->getRequestParameter('tags'), ',')) {
		
		  $TagsString = $this->getRequestParameter('tags');
			$TagsCut = explode(",", $TagsString);
		
	  	  } else {
		
		  $TagsCut[] = $this->getRequestParameter('tags');
		
		  }
			$twitterrag = '';

			foreach ($TagsCut as $TagCut) {
				
				$pat[0] = "/\s+/";
			
				$rep[0] = "";
			
				$str = preg_replace($pat,$rep,$TagCut);
				
				if ($str != '') {
				$tag = Tags::getTag($str);
				$newCompany2Tag = new Tag2Company();
				$newCompany2Tag->setTagId($tag->getId());
				$newCompany2Tag->setCompanyId($newCompany->getId());
				$newCompany2Tag->save();
				}
				
			  
			}
		  }		
		
		return $this->redirect('/thanks');
		

	    
	    }
 
 
 }
  
  public function executeEditCompany()
  
  {
  
    $this->company = $company = CompanyPeer::retrieveByPk($this->getRequestParameter('id'));


 if ($this->getRequest()->getMethod() != sfRequest::POST)
	    {
	      // display the form

	      $this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
	    } else {
	    
	    //edit a company
	    	    
	    $company->setName(htmlentities($this->getRequestParameter('name'),ENT_QUOTES,'utf-8'));
	    $company->setNameHebrew($this->getRequestParameter('hebrewname'));
	    $company->setDescription(htmlentities($this->getRequestParameter('description'),ENT_QUOTES,'utf-8'));
	    $company->setFounded($this->getRequestParameter('founded'));
	    $company->setEmail($this->getRequestParameter('email'));
	    $company->setLocation($this->getRequestParameter('location'));
	    $company->setUrl($this->getRequestParameter('url'));
	    $company->setBlogUrl($this->getRequestParameter('blogurl'));
	    $company->setRssUrl($this->getRequestParameter('rssurl'));
	    $company->setTwitterUser($this->getRequestParameter('twitteruser'));
	    $company->setPhoneNumber($this->getRequestParameter('phonenumber'));
	    $company->setRounds($this->getRequestParameter('rounds'));
	    $company->setFounders($this->getRequestParameter('founders'));
	    $company->setNumberEmploy($this->getRequestParameter('numberemploy'));
		$company->setAuthorId($this->getContext()->getUser()->getSignedUserId());
		$company->setPublishState($this->getRequestParameter('publishstate'));
		$company->setSlug(utils::toAscii(htmlentities($this->getRequestParameter('name'),ENT_QUOTES,'utf-8')));
		$company->save();
		
		//Lets create a new version
		
		//Create a shadow version
		
		//Change the last shadow from active to none.
		
		$cshadow = new Criteria();
		$cshadow->add(CompanyShadowPeer::COMPANY_ID,$company->getId());
		$cshadow->add(CompanyShadowPeer::STATUS,1);
		$cshadowc = CompanyShadowPeer::doSelectOne($cshadow);
		
		$cshadowc->setStatus(0);
		$cshadowc->save();
		
		$newversion = $cshadowc->getVersion();
		$newversion++;
		
		$newCompanyShadow = new CompanyShadow();
	    $newCompanyShadow->setName(htmlentities($this->getRequestParameter('name'),ENT_QUOTES,'utf-8'));	    	    
	    $newCompanyShadow->setNameHebrew(htmlentities($this->getRequestParameter('hebrewname'),ENT_QUOTES,'utf-8'));
	    $newCompanyShadow->setDescription(htmlentities($this->getRequestParameter('description'),ENT_QUOTES,'utf-8'));
	    $newCompanyShadow->setFounded($this->getRequestParameter('founded'));
	    $newCompanyShadow->setEmail($this->getRequestParameter('email'));
	    $newCompanyShadow->setLocation($this->getRequestParameter('location'));
	    $newCompanyShadow->setUrl($this->getRequestParameter('url'));
	    $newCompanyShadow->setBlogUrl($this->getRequestParameter('blogurl'));
	    $newCompanyShadow->setRssUrl($this->getRequestParameter('rssurl'));
	    $newCompanyShadow->setTwitterUser($this->getRequestParameter('twitteruser'));
	    $newCompanyShadow->setPhoneNumber($this->getRequestParameter('phonenumber'));
	    $newCompanyShadow->setRounds($this->getRequestParameter('rounds'));
	    $newCompanyShadow->setFounders($this->getRequestParameter('founders'));
	    $newCompanyShadow->setNumberEmploy($this->getRequestParameter('numberemploy'));
		$newCompanyShadow->setAuthorId($this->getContext()->getUser()->getSignedUserId());
		$newCompanyShadow->setPublishState($this->getRequestParameter('publishstate'));
		$newCompanyShadow->setSlug(utils::toAscii(htmlentities($this->getRequestParameter('name'),ENT_QUOTES,'utf-8')));
		$newCompanyShadow->setCompanyId($company->getId());
		$newCompanyShadow->setStatus(1);
		$newCompanyShadow->setVersion($newversion);
		$newCompanyShadow->save();
		
		//Save the new shadow and link it
		$company->setVersionshadow($newCompanyShadow->getId());
		$company->save();
		
		
	    if ($this->getRequest()->getFileName('logourl') != '') {
		
		$fileUploaded = $this->getRequest()->getFileName('logourl');
		//$fileEnd = explode(".", $fileUploaded);
		$fileend = utils::findexts($fileUploaded);
	    $fileName = $company->getName();
	    $fileName .= $company->getUpdatedAt();
	    $fileName .= '_thumb_';
		$fileName .= '.';
		$fileName .= $fileend;
		//$fileName .= 'jpg';
		$thumbnail = new sfThumbnail(105, 105, true, true, 105, 'sfGDAdapter');
		$thumbnail->loadFile($this->getRequest()->getFilePath('logourl'));
  		$thumbnail->save('images/companylogos/'.$fileName.'', 'image/jpeg');
	    
	 	$fileNameBig = $company->getName();
	 	$fileNameBig .= $company->getUpdatedAt();
	    $fileNameBig .= '_big_';
		$fileNameBig .= '.';
		$fileNameBig .= $fileend;
		//$fileNameBig .= 'jpg';
		$thumbnailbig = new sfThumbnail(225, 155, true, true, 155, 'sfGDAdapter');
		$thumbnailbig->loadFile($this->getRequest()->getFilePath('logourl'));
  		$thumbnailbig->save('images/companylogos/'.$fileNameBig.'', 'image/jpeg');

	    
	    
	    $company->setLogoUrl('/images/companylogos/'.$fileName.'');
	    $company->save();
	    
	    $newCompanyShadow->setLogoUrl('/images/companylogos/'.$fileName.'');
	    $newCompanyShadow->save();
	    
	    
		}

		//Lets create some tags for the seed funding
		
		
		  if ($this->getRequestParameter('rounds') == '') {
			//Lets do nothing bout the tags please
		  } else {
		   $seedrounds = $this->getRequestParameter('rounds');
		   $N = count($seedrounds);
		
		    //echo("You selected $N door(s): ");
		    for($i=0; $i < $N; $i++)
		    {
		      //echo($seedrounds[$i] . " ");
		      
		 	   $pat[0] = "/\s+/";
			
				$rep[0] = "";
			
				$str = preg_replace($pat,$rep,$seedrounds[$i]);
				
				if ($str != '') {
				$round = Tags::getTag($str);
				$newCompany2Tag = new Tag2Company();
				$newCompany2Tag->setTagId($round->getId());
				$newCompany2Tag->setCompanyId($newCompany->getId());
				$newCompany2Tag->save();
				}	
		    	
		    	
		    }

		  }	
		
		//Lets create some tags!
		
		
			  //Lets create some tags baby!
		
		  if ($this->getRequestParameter('tags') == '') {
			//Lets do nothing bout the tags please
		  } else {
		  $TagsCut = array();
		  
		  if (strstr($this->getRequestParameter('tags'), ',')) {
		
		  $TagsString = $this->getRequestParameter('tags');
			$TagsCut = explode(",", $TagsString);
		
	  	  } else {
		
		  $TagsCut[] = $this->getRequestParameter('tags');
		
		  }
			$twitterrag = '';

			foreach ($TagsCut as $TagCut) {
				
				$pat[0] = "/\s+/";
			
				$rep[0] = "";
			
				$str = preg_replace($pat,$rep,$TagCut);
				
				if ($str != '') {
				$tag = Tags::getTag($str);
				
				//check if the company is already tagged under those tags
				
				$tagcheck = new Criteria();
				$tagcheck->add(Tag2CompanyPeer::COMPANY_ID,$company->getId());
				$tagcheck->add(Tag2CompanyPeer::TAG_ID,$tag->getId());
				$tagCheckCheck = Tag2CompanyPeer::doSelectOne($tagcheck);
				
				if (!isset($tagCheckCheck)) {
				
				$newCompany2Tag = new Tag2Company();
				$newCompany2Tag->setTagId($tag->getId());
				$newCompany2Tag->setCompanyId($company->getId());
				$newCompany2Tag->save();
				
				}
				
				}
				
			  
			}
		  }		
		
		return $this->redirect('/');
		

	    
	    }


  
  }
  
  public function executeDelete()
  {
  
    $this->company = $company = CompanyPeer::retrieveByPk($this->getRequestParameter('id'));


 	if ($this->getRequest()->getMethod() != sfRequest::POST)
	    {
	      // display the form

	      $this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
	    } else {
	    
	    if ($this->getContext()->getUser()->isAuthenticated()) {

		//Delete the company 2 tag first
		$c = new Criteria();
		$c->add(Tag2CompanyPeer::COMPANY_ID,$company->getId());
		$tags = Tag2CompanyPeer::doDelete($c);
	
	    $company->delete();
	    return $this->redirect('/techindex');

	    }
	    
	    
	    }

  
  
  }
  
  public function executePermissions()
  {
  


	 		if ($this->getRequest()->getMethod() != sfRequest::POST)
		    {
		      // display the form
			 
		      $this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
				
			$c = new Criteria();
			$c->addAscendingOrderByColumn(UserPeer::ID);
		
			$pager = new sfPropelPager('User', 150);
			$pager->setCriteria($c);
			$pager->setPage($this->getRequestParameter('page', 1));
			$pager->init();
			$this->pager = $pager;
	
	
		    } else {
	
	
	
	
			}
		  
  
  }
  
  
  public function executeEditPermission()
  {
  


	 		if ($this->getRequest()->getMethod() != sfRequest::POST)
		    {
		      // display the form
			 
		      $this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
				
			$this->user = UserPeer::retrieveByPk($this->getRequestParameter('id'));
	
	
		    } else {
	
			$user = UserPeer::retrieveByPk($this->getRequestParameter('id'));

			
			//add to db also
			
			//check if user has existing permissions
			
			$c = new Criteria();
			$c->add(User2PermissionPeer::USER_ID,$user->getId());
			$checkpermission = User2PermissionPeer::doSelectOne($c);
			
			if (!isset($checkpermission)) {
			
			
			$newPermission = new User2Permission();
			$newPermission->setUserId($user->getId());
			$newPermission->setPermission($this->getRequestParameter('permission'));
			$newPermission->save();
			
			
			} else {
			
			$checkpermission->setPermission($this->getRequestParameter('permission'));
			$checkpermission->save();
			
			}
	
			return $this->redirect('/user/permissions');

	
			}
		  
  
  }
  
  
    
  public function executeDrafts()
  {
  


	 		if ($this->getRequest()->getMethod() != sfRequest::POST)
		    {
		      // display the form
			 
		     $this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
			$c = new Criteria();
			$c->add(CompanyPeer::PUBLISHSTATE,'draft');
			$c->addAscendingOrderByColumn(CompanyPeer::NAME);
		
			$pager = new sfPropelPager('Company', 150);
			$pager->setCriteria($c);
			$pager->setPage($this->getRequestParameter('page', 1));
			$pager->init();
			$this->pager = $pager;
			
		    } else {
	
	
	
			}
	
  
  
  }
  
  public function executeAddCred()
  {
  	
      $this->getContext()->getUser()->addCredential('admin');
      
      $user = UserPeer::retrieveByPk($this->getContext()->getUser()->getSignedUserId());
      //add permission to the permission table.
				
		//check if user has existing permissions
		
		$c = new Criteria();
		$c->add(User2PermissionPeer::USER_ID,$user->getId());
		$checkpermission = User2PermissionPeer::doSelectOne($c);
		
		if (!isset($checkpermission)) {
		
		
		$newPermission = new User2Permission();
		$newPermission->setUserId($user->getId());
		$newPermission->setPermission('admin');
		$newPermission->save();
		
		
		} else {
		
		$checkpermission->setPermission('admin');
		$checkpermission->save();
		
		}
			
			      
      echo $this->getContext()->getUser()->hasCredential('admin');
      
  	  exit();
  }
  
  public function executeThankYou()
  {
  
  
  }
}
